/**
 * @filename:StudentClassServiceImpl StudentClass
 * @project constract  V1.0
 * Copyright(c) 2018 lin keqing Co. Ltd.
 * All right reserved.
 */
package com.znufe.exam.service.impl;

import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.znufe.exam.domain.User;
import com.znufe.exam.domain.classes.StudentClassResponseObject;
import com.znufe.exam.repository.StudentClassMapper;
import com.znufe.exam.service.enums.ActionEnum;
import com.znufe.exam.utility.ModelMapperSingle;
import com.znufe.exam.viewmodel.admin.classes.StudentClassCreateVM;
import com.znufe.exam.viewmodel.admin.classes.StudentClassPageRequestVM;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.znufe.exam.domain.StudentClass;
import com.znufe.exam.service.StudentClassService;

/**
 *
 * @Description:  班级信息表——SERVICEIMPL
 * @Author:       lin keqing
 * @CreateDate:   StudentClass
 * @Version:      V1.0
 *
 */
@Service
public class StudentClassServiceImpl extends BaseServiceImpl<StudentClass> implements StudentClassService {

    protected final static ModelMapper modelMapper = ModelMapperSingle.Instance();

    private final StudentClassMapper studentClassMapper;

	@Autowired
	public StudentClassServiceImpl(StudentClassMapper studentClassMapper) {
		super(studentClassMapper);
		this.studentClassMapper = studentClassMapper;
	}

	// 通过主键查询对象
	@Override
	public StudentClass selectByPrimaryKey(Integer id) {
		return studentClassMapper.selectByPrimaryKey(id);
	}

	// 通过班级名称查询对象
    @Override
    public StudentClass selectByClassName(String className) {
        return studentClassMapper.selectByClassName(className);
    }

	// 通过主键删除对象
	@Override
	public int deleteByPrimaryKey(Integer id) {
		return studentClassMapper.deleteByPrimaryKey(id);
	}

	// 添加对象
	@Override
	public int insertSelective(StudentClass studentClass) {
		return studentClassMapper.insertSelective(studentClass);
	}

	// 修改对象
	@Override
	public int updateByPrimaryKeySelective(StudentClass studentClass) {
		return studentClassMapper.updateByPrimaryKeySelective(studentClass);
	}

	// edit接口对应的Service层代码，进行新增、更新的操作
	@Override
    public StudentClass saveStudentClassFromVM(StudentClassCreateVM vm, User user) {
        ActionEnum actionEnum = vm.getId() == null ? ActionEnum.ADD : ActionEnum.UPDATE;
        Date now = new Date();

        StudentClass studentClass;
        if (actionEnum == ActionEnum.ADD) {
            studentClass = modelMapper.map(vm, StudentClass.class);
            studentClass.setCreateUserId(user.getId());
            studentClass.setIsEnabled(1);
            studentClass.setIsDeleted(0);
            studentClass.setCreateTime(now);
            studentClassMapper.insertSelective(studentClass);
        } else {
            studentClass = studentClassMapper.selectByPrimaryKey(vm.getId());
            studentClass.setClassName(vm.getClassName());
			studentClass.setDescription(vm.getDescription());
			studentClass.setModifiedTime(now);
            studentClassMapper.updateByPrimaryKeySelective(studentClass);
        }
        return studentClass;
    }

    // 选择查询班级信息（班级名称）
    @Override
    public PageInfo<StudentClassResponseObject> studentClassPage(StudentClassPageRequestVM requestVM) {
		return PageHelper.startPage(requestVM.getPageNum(), requestVM.getPageSize(),
                    "id desc").doSelectPageInfo(() ->
                    studentClassMapper.studentClassPage(requestVM));
	}


	//查询集合
	@Override
	public List<StudentClass> queryStudentClassList(StudentClass studentClass) {
		return studentClassMapper.queryStudentClassList(studentClass);
	}

	@Override
	public Integer selectClassStuCountById(Integer classId) {
		return studentClassMapper.selectClassStuCountById(classId);
	}

}
